import router from "@/router/index.ts";
import Nprogress from "nprogress";
import "nprogress/nprogress.css";
import useUserStore from "@/store/modules/user.ts";
import pinia from "@/store";
let userStore = useUserStore(pinia);
let whiteList = [
  "/",
  "/home",
  "/hospital/register",
  "/hospital/detail",
  "/hospital/notice",
  "/hospital/close",
  "/hospital/search",
  "/login",
  "/register",
];
Nprogress.configure({
  showSpinner: false, // 是否显示加载进度条
});

// 前置路由
router.beforeEach((to, from, next) => {
  // 访问之前
  Nprogress.start();
  document.title = "萧曵_尚医通_" + to.meta.title;
  if (userStore.userInfo.token || whiteList.includes(to.path)) {
    next();
  } else {
    // 用户未登录,访问不需要鉴权的地方
    userStore.dialogFormVisible = true;
    next({ path: "/home", query: { redirect: to.fullPath } });
  }
});
// 后置路由
router.afterEach((to, from) => {
  // 访问路由成功
  Nprogress.done();
});
